<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model common\models\Product */
/* @var $form yii\widgets\ActiveForm */

?>
<style>
    label{font-weight:400!important;}
</style>
<div class="product-form">

    <?php $form = ActiveForm::begin([
            'id'   => $model->formName(),
//            'action' => Url::to(['save']),
            'options'=>['enctype'=>'multipart/form-data'],
//            'validationUrl'=>'validate',
//            'enableAjaxValidation'=>true
    ]); ?>
    <?= $form->field($model, 'ttid')->textInput(['maxlength' => true]) ?>
    <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'remark')->textInput(['maxlength' => true]) ?>
    <?= $form->field($model, 'notes')->textInput(['maxlength' => true]) ?>
    <?= $form->field($model, 'price')->textInput(['maxlength' => true])->label("商品价格") ?>

    <?= $form->field($model, 'newprice')->textInput(['maxlength' => true])->label("券后价格") ?>

    <?=Html::img($model->image,['id'=>'image'])?>
    <?= $form->field($model, 'pic')->fileInput(['accept'=>'image/*','value'=>$model->image]) ?>
    <?= Html::hiddenInput("Product[image]",$model->image,['id'=>'image_value'])?>

    <?= $form->field($model, 'coupon_id')->dropDownList($data,['promt'=>$model->coupon_id]) ?>

    <?= $form->field($model, 'description')->textInput() ?>

    <?= $form->field($model, 'share_title')->textInput() ?>

    <?=Html::img($model->share_img,['id'=>'share_img'])?>
    <?= $form->field($model, 'pic1')->fileInput(['accept'=>'image/*','value'=>$model->share_img]) ?>
    <?= Html::hiddenInput("Product[share_img]",$model->share_img,['id'=>'share_img_value'])?>

    <?= $form->field($model, 'share_txt')->textInput() ?>

    <?= $form->field($model, 'share_url')->textInput() ?>
    <?= $form->field($model, 'id')->hiddenInput()->label(false) ?>
<!--    <div class="form-group">-->
<!--        --><?////= Html::submitButton('保存', ['class' => 'btn btn-success']) ?>
<!--        --><?//= Html::button('保存', ['class' => 'btn btn-success']) ?>
<!--    </div>-->

    <?php ActiveForm::end(); ?>

</div>
<script src="/admin/js/jquery.js"></script>
<script src="/admin/js/ajaxfileupload.js"></script>
<script>

    function upload() {
        $('#product-pic').on('change', function() {
            var file = $(this)[0].files[0];
            var accept = $(this).attr("accept");
            console.log(file);
            if (file) {
                if (accept.indexOf(file.type.split("/")[0]) > -1) {

                        uploadfile();
                }
            }

        });
         $('#product-pic1').on('change', function() {
             var file = $(this)[0].files[0];
             var accept = $(this).attr("accept");
             console.log(file);
             if (file) {
                 if (accept.indexOf(file.type.split("/")[0]) > -1) {

                     uploadfile1();
                 }
             }

         })
    }

    function uploadfile() {

        $.ajaxFileUpload({
            url: "/admin/product/upload",
            secureuri: false,
            fileElementId: "product-pic",
            dataType: 'json',
            success: function(data) {

                if (data.result == "success") {
                    srcUrl = data.url;
                    $("#image").attr("src",srcUrl);
                    $("#image_value").val(srcUrl);
                } else {
                    alert(data.msg);

                }
                upload()
                // $('#chief_img').removeAttr("disabled");
            },
            error: function(data, status, e) {
                alert('网络错误！请从新上传图片');
                console.log(data)
                upload();
                // $('#chief_img').removeAttr("disabled");
            }
        })
    }
    function uploadfile1() {

        $.ajaxFileUpload({
            url: "/admin/product/upload1",
            secureuri: false,
            fileElementId: "product-pic1",
            dataType: 'json',
            success: function(data) {

                if (data.result == "success") {
                    srcUrl = data.url;
                    $("#share_img").attr("src",srcUrl);
                    $("#share_img_value").val(srcUrl);
                } else {
                    alert(data.msg);

                }
                upload()
                // $('#chief_img').removeAttr("disabled");
            },
            error: function(data, status, e) {
                alert('网络错误！请从新上传图片');
                console.log(data)
                upload();
                // $('#chief_img').removeAttr("disabled");
            }
        })
    }
    upload();

</script>

